const express = require('express')
const router = express.Router()
const bcrypt = require('bcrypt')
const { UserModel } = require('../model/index')
const jwt = require('jwt-simple')
const { setToken } = require('../utils/token')

router.post('/update', (req, res, next) => {
  const { username, password, id } = req.body
  bcrypt.hash(password, 10, (err, hash) => {
    if (err) {
      res.json({
        code: 400,
        data: {
          message: err.message
        }
      })
      return
    }
  UserModel.updateOne({id}, { username, password:hash })
    .then(resData => {
      res.json({
        code: 200,
        data: {
          status:200,
          msg:'修改成功',
          data:{
            resData
          }
        }
      })
    }).catch(err => {
      res.json({
        code:400,
        data: {
          msg:'修改失败'
        }
      })
    })
  })
})

module.exports = router